<?php

class IndexController extends Zend_Controller_Action
{
    private $base_url;
	
	
    public function init()
    {
        //L'index Controller se charge du login, donc on charge le layout de login

		
		
        $this->_helper->layout->setLayout('Login');
        $this->base_url = $this->getRequest()->getBaseUrl();
    }

    public function indexAction()
    {
        $form_auth=new Application_Form_Authentification(null, $this->base_url);
        $auth = Zend_Auth::getInstance();

		/*$frontendOptions = array(
			'lifetime' => 7200, // temps de vie du cache de 2 heures
			'automatic_serialization' => true
		 );
		  
		 $backendOptions = array(
			 // Répertoire où stocker les fichiers de cache
			 'cache_dir' => '/tmp/php/'
		 );
		  
		 // créer un objet Zend_Cache_Core
		 $cache = Zend_Cache::factory('Page',
									  'File',
									  $frontendOptions,
									  $backendOptions);
		$cache->start();*/
		
		
        if (!$auth->hasIdentity()){
            if($this->getRequest()->isPost()){
                // Affichage du formulaire de contact
                $form_data = $this->getRequest()->getPost();

                if($form_auth->isValid($form_data)){
                    // Démarche d'authentification

                    // Contrôle 1 : on vérifie par rapport à table utilisateur
                    $dbAdapter = Zend_Db_Table::getDefaultAdapter();
                    $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter, 
                                                                    Zend_Registry::get('TBL_PREFIXE').'utilisateur', 
                                                                    'login', 
                                                                    'password',
                                                                    'MD5(?)');
                    $authAdapter->setIdentity($form_auth->getValue('login'));
                    $authAdapter->setCredential($form_auth->getValue('password'));

                    $resultAuth=$auth->authenticate($authAdapter);

                    // Si trouvé
                    if($resultAuth->isValid()){
                        $auth->getStorage()->write((array) $authAdapter->getResultRowObject(null, 'password'));
                        $auth->getStorage()->write(array('privilege' => 'admin'));
                        
                        // Redirection vers layout admin
                        $this->_redirect('/commandes/index');
                    }else{
                        // Sinon contrôle 2 : on vérifie par rapport à table client
                        $dbAdapter = Zend_Db_Table::getDefaultAdapter();
                        $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter, 
                                                                        Zend_Registry::get('TBL_PREFIXE').'client', 
                                                                        'login', 
                                                                        'password',
                                                                        'MD5(?)');
                        $authAdapter->setIdentity($form_auth->getValue('login'));
                        $authAdapter->setCredential($form_auth->getValue('password'));

                        $resultAuth=$auth->authenticate($authAdapter);

                        if($resultAuth->isValid()){
                            $auth->getStorage()->write((array) $authAdapter->getResultRowObject(null, 'password'));
                            $auth->getStorage()->write(array('privilege' => 'client'));
                            
                            // Redirection vers layout client
                            $this->_redirect('/client/index');
                        }else{
                            $this->view->erreur = "Identifiants incorrects";
                            $this->view->form_auth = $form_auth;
                        }

                    }
                }else{
                    $form_auth->populate($form_data);
                    $this->view->form_auth = $form_auth;
                }
            }else{
                    // Sinon*/
                    $this->view->form_auth = $form_auth;
            }
        }else{
                Zend_Auth::getInstance()->clearIdentity();
                $this->_redirect('/index');
                exit;
        }
    }
    
    public function logoutAction(){
        // action body
        $this->_helper->layout->disableLayout();
        Zend_Auth::getInstance()->clearIdentity();
        /*$this->_redirect('/index');
        exit;*/
    }


}

